package org.sny7.openOfflineList.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;

import org.sny7.openOfflineList.util.LanguageChanger;

public class GameListSearcher extends JPanel {
	
	private static GameListSearcher gameListSearcher = init();
	
	private static JTextField searchTextField;
	
	private static List<String> searchHistory = new ArrayList<String>();
	
	private static JPopupMenu searchHistoryMenu;

	private static final long serialVersionUID = 1L;
	
	private GameListSearcher() {
		
	}
	
	public static GameListSearcher getGameListSearcher() {
		return gameListSearcher;
	}
	
	private static GameListSearcher init() {
		GameListSearcher gameListSearcher = new GameListSearcher();
		gameListSearcher.setLayout(new BorderLayout());
		
		searchTextField = new JTextField();
		LanguageChanger.localeThis(searchTextField, "searchTextField-ToolTipText");
		searchTextField.addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == 10) {
					search();
				}
			}
			public void keyReleased(KeyEvent e) {
			}
			public void keyTyped(KeyEvent e) {
			}
		});
		gameListSearcher.add(searchTextField, BorderLayout.CENTER);

		searchHistoryMenu = new JPopupMenu();
		
		final JButton searchButton = new JButton();
		LanguageChanger.localeThis(searchButton, "searchButton-Text?searchButton-ToolTipText");
		searchButton.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent e) {
				switch (e.getButton()) {
				case MouseEvent.BUTTON1 :
					search();
					break;
				case MouseEvent.BUTTON3 :
					if (searchHistory.size() > 0) {
						searchHistoryMenu.show(searchButton, 0, searchButton.getHeight());
					}
				}
			}
			public void mouseEntered(MouseEvent e) {
			}
			public void mouseExited(MouseEvent e) {
			}
			public void mousePressed(MouseEvent e) {
			}
			public void mouseReleased(MouseEvent e) {
			}
		});
		gameListSearcher.add(searchButton, BorderLayout.EAST);
		
		return gameListSearcher;
	}
	
	private static void search() {
		final String text = searchTextField.getText().trim();
		if (text.equals("")) {
			return;
		}
		GameList.search(text);
		if (!searchHistory.contains(text)) {
			searchHistory.add(text);
			JMenuItem menuItem = new JMenuItem();
			menuItem.setText(text);
			menuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					searchTextField.setText(text);
					GameList.search(text);
				}
			});
			searchHistoryMenu.add(menuItem);
		}
	}

}
